/*******************************************************************************
*  file    : gsmmodem.hpp
*  created : 05.08.2013
*  author  : Slyshyk Oleksiy (alexSlyshyk@gmail.com)
*******************************************************************************/

#ifndef GSMMODEM_HPP
#define GSMMODEM_HPP

#include "gsm_global.hpp"
#include <QObject>
#include <QSerialPort>
#include <QQueue>
#include <QStringList>
#include <qatresult.h>

class QAtChat;
class QTimer;
class GsmSmsReader;

class GSMSHARED_EXPORT GsmModem : public QObject
{
    Q_OBJECT
public:
    enum ErrorCodes
    {
        NO_ERROR     = 0,
        PORT_ERROR   = 1,
        ATCHAT_ERROR = 2
    };
public:
    explicit GsmModem(QObject *parent = 0);
    ~GsmModem();

    virtual void init    (const QString &name, int baudrate);
    bool     openSerial  (const QString &name);
    QAtChat* primaryAtChat();
    void     chat( const QString& command );
    void     chat( const QString& command, QObject *target, const char *slot,
                   QAtResult::UserData *data = 0 );

    QString manufacturer() const;
    QString model       () const;
    QString revision    () const;
    QString number      () const;

    QString errorString () const;
    bool    isValid     () const;


private slots:
    void     onModemInitResponce(bool, const QAtResult& res);
    void     onModemInitTimeout ();
    void     onSerialError      (QSerialPort::SerialPortError err);
    void     onChatDead         ();
signals:
    void initialized(bool);
    void error(int);
protected:
    bool initialized_;
    bool initializing_;
private:
    QStringList                 init_list_;
    int                         init_pos_;
    QSerialPort*                serial_ ;
    QAtChat*                    at_chat_;
    int                         serial_baudrate_;

    QString                     errorString_;
    QTimer*                     timer_;

private:
    QString                     manufacturer_;
    QString                     model_;
    QString                     revision_;
    QString                     number_;

};


#endif // GSMMODEM_HPP
